package com.link.base.base.performance.service;

import com.link.base.base.performance.dao.mybatis.mapper.ScoringDetailsMapper;
import com.link.base.base.performance.model.Performance;
import com.link.base.base.performance.model.ScoringDetails;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 考核打分明细
 *
 * @author 王昊
 * @date 2020-08-31 17:40:08
 */
@Service
public class ScoringDetailsServiceImpl extends BasicServiceImpl<ScoringDetails> implements ScoringDetailsService {

    @Resource
    private ScoringDetailsMapper    scoringDetailsMapper;

    @Resource
    private PerformanceService performanceService;

    @Override
    public BasicMapper<ScoringDetails> getBasicMapper() {
        return scoringDetailsMapper;
    }

    @Override
    public void deleteByPerfoId(ScoringDetails sco) {
        scoringDetailsMapper.deleteByPerfoId(sco);
    }

    /**
     * 用户双击编辑考核打分明细，点击保存时校验成绩是否在范围内，后将成绩加和至头上考核总分
     * @author 王昊
     * @date 13:06 2021/3/22
     * @param entity
     * @return void
     **/
    @Override
    public void beforUpdate(ScoringDetails entity) throws Exception {
        super.beforUpdate(entity);
        //20210413逻辑修改！！-->20210525修改
        //最终得分：等于本项得分*权重占比+额外加分
        BigDecimal score = entity.getScore() == null ? BigDecimal.ZERO : entity.getScore();
        BigDecimal keyPoints = entity.getKeyPoints() == null ? BigDecimal.ZERO : entity.getKeyPoints();
        BigDecimal extraCredit = entity.getExtraCredit() == null ? BigDecimal.ZERO : entity.getExtraCredit();
        entity.setItemScore(score.multiply(keyPoints).add(extraCredit));
        scoringDetailsMapper.update(entity);
        //考核得分总计：所有考核打分明细行的“最终得分”相加之和，来源：考核打分明细表。
        //开发思路：每保存一次行，根据绩效考核id查询对应打分记录，遍历记录加和成绩至头上考核的分总计字段
        ScoringDetails queryScor = new ScoringDetails();
        queryScor.setPerformanceId(entity.getPerformanceId());
        List<ScoringDetails> scoringDetails = scoringDetailsMapper.queryByPerfomanceId(queryScor);
        BigDecimal assessmentScore = new BigDecimal(0);
        if (scoringDetails != null && !scoringDetails.isEmpty()) {
            for (ScoringDetails scoringDetail : scoringDetails) {
                BigDecimal itemScore  = scoringDetail.getItemScore() == null ? BigDecimal.ZERO : scoringDetail.getItemScore();
                //计算总成绩
                assessmentScore = assessmentScore.add(itemScore);
            }
        }
        //找绩效考核头
        Performance queryPerf = new Performance();
        queryPerf.setId(entity.getPerformanceId());
        Performance performance = performanceService.queryById(queryPerf);
        if (performance != null) {
            //塞入总成绩
            performance.setAssessmentScore(assessmentScore);
        }

        //最终考核得分系数：根据考核得分总计自动生成。
        //若考核岗位为综合科、核价科，系数值：考核得分135-150，赋值1.5；120-134.9，赋值1.2；105-119.9，赋值1.0；90-104.9，赋值0.6；0-89.9，赋值0。
        //若考核岗位为业务经理，考核得分90-100，赋值1.5；80-89.9，赋值1.2；70-79.9，赋值1.0；60-69.9，赋值0.6；0-59.9，赋值0。
        //根据考核得分总计判断为考核得分系数赋值
        String positionStr = performance.getPosition();
        if ("IntegratedSection".equals(positionStr) || "NuclearSection".equals(positionStr)) {
            if (assessmentScore.compareTo(new BigDecimal(150)) < 1 && assessmentScore.compareTo(new BigDecimal(135)) > -1) {
                //135<= x <= 150，赋值1.5  左闭右闭
                performance.setScoreCoefficient(new BigDecimal(1.5));
            } else if (assessmentScore.compareTo(new BigDecimal(135)) == -1 && assessmentScore.compareTo(new BigDecimal(120)) > -1) {
                //120 <= x < 135，赋值1.2  左闭右开
                performance.setScoreCoefficient(new BigDecimal(1.2));
            } else if (assessmentScore.compareTo(new BigDecimal(120)) == -1 && assessmentScore.compareTo(new BigDecimal(105)) > -1) {
                //105 <= x < 120，赋值1.0  左闭右开
                performance.setScoreCoefficient(new BigDecimal(1.0));
            } else if (assessmentScore.compareTo(new BigDecimal(105)) == -1 && assessmentScore.compareTo(new BigDecimal(90)) > -1) {
                //90-105，赋值0.6  左闭右开
                performance.setScoreCoefficient(new BigDecimal(0.6));
            } else if (assessmentScore.compareTo(new BigDecimal(90)) == -1 && assessmentScore.compareTo(new BigDecimal(0)) > -1) {
                //0-90，赋值0  左闭右开
                performance.setScoreCoefficient(new BigDecimal(0));
            } else {
                //考核得分总计出错，请联系管理员！
                throw new ServiceException("IYUPERFORM-011");
            }
        } else if ("BusinessManager".equals(positionStr)) {
//            考核得分90-100，赋值1.5；80-89.9，赋值1.2；70-79.9，赋值1.0；60-69.9，赋值0.6；0-59.9，赋值0
            if (assessmentScore.compareTo(new BigDecimal(100)) < 1 && assessmentScore.compareTo(new BigDecimal(90)) > -1) {
                //90<= x <= 100，赋值1.5  左闭右闭
                performance.setScoreCoefficient(new BigDecimal(1.5));
            } else if (assessmentScore.compareTo(new BigDecimal(90)) == -1 && assessmentScore.compareTo(new BigDecimal(80)) > -1) {
                //80<= x < 90，赋值1.2  左闭右开
                performance.setScoreCoefficient(new BigDecimal(1.2));
            } else if (assessmentScore.compareTo(new BigDecimal(80)) == -1 && assessmentScore.compareTo(new BigDecimal(70)) > -1) {
                //70 <= x < 80，赋值1.0  左闭右开
                performance.setScoreCoefficient(new BigDecimal(1.0));
            } else if (assessmentScore.compareTo(new BigDecimal(70)) == -1 && assessmentScore.compareTo(new BigDecimal(60)) > -1) {
                //60-70，赋值0.6  左闭右开
                performance.setScoreCoefficient(new BigDecimal(0.6));
            } else if (assessmentScore.compareTo(new BigDecimal(60)) == -1 && assessmentScore.compareTo(new BigDecimal(0)) > -1) {
                //0-60，赋值0  左闭右开
                performance.setScoreCoefficient(new BigDecimal(0));
            } else {
                //考核得分总计出错，请联系管理员！
                throw new ServiceException("IYUPERFORM-011");
            }
        } else {
            //获取到的绩效考核头岗位类型非法,请联系管理员！
            throw new ServiceException("IYUPERFORM-012");
        }
        performanceService.update(performance);









        //20210322逻辑修改
        //根据规则判断各项成绩是否在范围内
        //  会议制度的两个考核项总分相加不超过18分  meetingSystem
        //  合同签订率的三个考核项总分相加不超过24分  contractSignRate
        // 贷款回笼进度的两个考核项总分相加不超过42分  progressWithdrawal
        // 模具开票的四个考核项总分不超过24分 modInvoice
        // 其他领导布置的任务的两个考核项不超过12分  otherLeaderTasks
        //  根据头id查询本次考核的所有成绩，遍历这些记录加和，看是否超过对应分数
//        ScoringDetails queryScore = new ScoringDetails();
//        queryScore.setPerformanceId(entity.getPerformanceId());
//        queryScore.setIndexId(entity.getIndexId());
//        List<ScoringDetails> scoringDetails = scoringDetailsMapper.queryByPerfomanceId(queryScore);
//        if (scoringDetails.isEmpty() || scoringDetails == null) {
//            //获取考核打分明细出错，请重新生成本次考核！
//            throw new ServiceException("IYUPERFORM-005");
//        }
//        //判断当前行隶属于哪个考核大类，将成绩放入对应变量
//        BigDecimal zero = new BigDecimal(0);
//        String assessmentItems = entity.getAssessmentItems() == null ? null : entity.getAssessmentItems();
//        BigDecimal nowScore = entity.getScore() == null ? new BigDecimal(0) : entity.getScore();
//        BigDecimal meetingSystem = "meetingSystem".equals(assessmentItems) ? nowScore : zero;
//        BigDecimal contractSignRate = "contractSignRate".equals(assessmentItems) ? nowScore : zero;
//        BigDecimal progressWithdrawal = "progressWithdrawal".equals(assessmentItems) ? nowScore : zero;
//        BigDecimal modInvoice = "modInvoice".equals(assessmentItems) ? nowScore : zero;
//        BigDecimal otherLeaderTasks = "otherLeaderTasks".equals(assessmentItems) ? nowScore : zero;
//        for (ScoringDetails scoringDetail : scoringDetails) {
//            BigDecimal score = scoringDetail.getScore() == null ? new BigDecimal(0) : scoringDetail.getScore() ;
//            if ("meetingSystem".equals(scoringDetail.getAssessmentItems())) {
//                meetingSystem = meetingSystem.add(score);
//            } else if ("contractSignRate".equals(scoringDetail.getAssessmentItems())) {
//                contractSignRate = contractSignRate.add(score);
//            } else if ("progressWithdrawal".equals(scoringDetail.getAssessmentItems())) {
//                progressWithdrawal = progressWithdrawal.add(score);
//            } else if ("modInvoice".equals(scoringDetail.getAssessmentItems())) {
//                modInvoice = modInvoice.add(score);
//            } else {
//                otherLeaderTasks = otherLeaderTasks.add(score);
//            }
//        }
//        if (meetingSystem.compareTo(new BigDecimal(18)) == 1) {
//            //会议制度的两个考核项总分相加不超过18分！请重新输入
//            throw new ServiceException("IYUPERFORM-006");
//        }
//        if (contractSignRate.compareTo(new BigDecimal(24)) == 1) {
//            //合同签订率的三个考核项总分相加不超过24分！请重新输入
//            throw new ServiceException("IYUPERFORM-007");
//        }
//        if (progressWithdrawal.compareTo(new BigDecimal(42)) == 1) {
//            //贷款回笼进度的两个考核项总分相加不超过42分！请重新输入
//            throw new ServiceException("IYUPERFORM-008");
//        }
//        if (modInvoice.compareTo(new BigDecimal(24)) == 1) {
//            //模具开票的四个考核项总分不超过24分！请重新输入
//            throw new ServiceException("IYUPERFORM-009");
//        }
//        if (otherLeaderTasks.compareTo(new BigDecimal(12)) == 1) {
//            //其他领导布置的任务的两个考核项不超过12分！请重新输入
//            throw new ServiceException("IYUPERFORM-010");
//        }
//        //根据头id查询绩效考核项，将成绩加和至考核总分
//        Performance queryPerform = new Performance();
//        queryPerform.setId(entity.getPerformanceId());
//        Performance performance = performanceService.queryById(queryPerform);
//        if (Objects.isNull(performance)) {
//            throw new ServiceException("头行关联出错请联系管理员！");
//        }
//        BigDecimal assessmentScore;
//        assessmentScore = meetingSystem.add(contractSignRate).add(progressWithdrawal).add(modInvoice).add(otherLeaderTasks);
//        performance.setAssessmentScore(assessmentScore);
//        performanceService.update(performance);
    }


    //    public void beforUpsert(ScoringDetails entity) throws Exception {
//        super.beforUpsert(entity);
//        //编辑完本项得分后，计算【考核大类总分】以及【考核得分总计】
//        //1.【考核大类总分】字段，每一个考核大类的总分均为考核大类中，所有本项得分的总和
//        //根据当前行记录的考核大类，绩效考核id查询该大类的所有考核指标，加和所有值
//        //需要先将当前行录入的成绩更新到数据库，确保后面查到的当前行记录的成绩是最新成绩
//        scoringDetailsMapper.update(entity);
//        //查询当前绩效考核，同一考核大类的记录
//        List<ScoringDetails> scoringDetails = scoringDetailsMapper.queryByPerfoId(entity);
//        BigDecimal score = new BigDecimal(0);
//        BigDecimal zero =  new BigDecimal(0);
//        BigDecimal score1 ;
//        for (ScoringDetails scoringDetails1 : scoringDetails) {
//            score1 = scoringDetails1.getScore() == null ? zero : scoringDetails1.getScore();
//            score = score.add(score1);
//        }
//        //为刚查出来的大类的【考核大类总分】附和值
//        for (ScoringDetails scoringDetails1 : scoringDetails) {
//            scoringDetails1.setItemScore(score);
//            scoringDetailsMapper.update(scoringDetails1);
//        }
//        //当scoringDetails1 等于 entity 时，itemScore 的值，会被后面的update(entity)方法覆盖，因此直接再给entity覆盖score
//        entity.setItemScore(score);
//
//        //2.【考核得分总计】值的计算方式：M==KPI项考核大类总分*100%+日常工作项考核大类总分*30%+行为规范考核大类总分*20%`
//        /**
//         * 根据考核ID，在打分明细表找到打分项，返回list遍历list，判断考核大类类型
//         * 当为routine，将拿到的打分*0,3
//         */
//        //根据绩效考核id，查询所有本次考核自评记录
//        List<ScoringDetails> scoringDetails2 = scoringDetailsMapper.queryByPerfomanceId(entity);
//        BigDecimal kpi = new BigDecimal(0) ;
//        BigDecimal routine =  new BigDecimal(0);
//        BigDecimal code =  new BigDecimal(0);
//        BigDecimal score2;
//        for (ScoringDetails scoringDetails1 : scoringDetails2) {
//            score2 = scoringDetails1.getScore() == null ? zero : scoringDetails1.getScore();
//            if ("Routine".equals(scoringDetails1.getAssessmentItems())) {
//                routine = routine.add(score2);
//            } else if ("Code".equals(scoringDetails1.getAssessmentItems())) {
//                code = code.add(score2);
//            } else {
//                kpi = kpi.add(score2);
//            }
//        }
//        Performance per = new Performance();
//        per.setId(entity.getPerformanceId());
//        Performance performance = performanceService.queryById(per);
//        //新建一个绩效考核，此时performanceid去查询头，可能由于数据已生成但未插入数据库，导致查不出
//        if (performance != null) {
//            performance.setAssessmentScore(kpi.add(routine.multiply(new BigDecimal(0.3))).add(code.multiply(new BigDecimal(0.2))));
//            performanceService.update(performance);
//        }
//
//    }
}
